package com.zhhl.util;

import com.alibaba.fastjson.JSON;
import com.jld.base.common.bean.business.RedisBean;
import com.jld.common.log4j.JLDLogger;
import com.jld.common.util.AppContent;
import com.jld.common.util.StringUtil;
import com.zhhl.redis.RedisManageService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;

public class RedisJob
{

  @Autowired
  private static RedisManageService redisManageService;

  public static RedisManageService getRedisManageService()
  {
    if (redisManageService == null) {
      redisManageService = (RedisManageService)AppContent.getContextBean("redisManageService");
    }
    return redisManageService;
  }

  public void doRedisUp()
  {
    doJson();
  }

  public static void doJson()
  {
    String DPURL = PropertieUtils.getString("dapan.url");
    String resultString = "";
    HttpClient hc = new HttpClient(DPURL, 30000, 30000);
    try {
      int status = hc.send(null, "GB2312");
      if (200 == status) {
        resultString = hc.getResult();
      }

      resultString = resultString.replaceAll("\r\n", "");

      Map allmap = new HashMap();
      Map price_list = new HashMap();

      String[] total = resultString.split("\n");
      if (total.length > 0)
      {
        for (int j = 0; j < total.length - 1; j++) {
          String temp = total[j];
          if ((StringUtil.isNotEmpty(temp)) && (temp.length() > 0)) {
            List list_map = new ArrayList();
            String[] linetemp = temp.split(",");
            for (int i = 0; i < linetemp.length; i++)
            {
              list_map.add(linetemp[i]);
            }
            price_list.put(linetemp[1], list_map);
          }
        }
        if (total.length == 34) {
          allmap.put("price_date", total[33]);
          allmap.put("price_list", price_list);
        }
      }

      String sendJsonStr = JSON.toJSONString(allmap, false);
      getRedisManageService().delete("HQ_TradeWeb_DATA");
      RedisBean addBean = new RedisBean("HQ_TradeWeb_DATA", sendJsonStr);
      getRedisManageService().add(addBean);
      JLDLogger.debug("==============成功写入Redis 缓存系统中==========写入内容【：" + addBean.getValue() + "】");
      UtilTools.writeFile(PropertieUtils.getString("js.FilePath"), addBean.getValue().toString());
    } catch (Exception e) {
      JLDLogger.debug("=======写入Redis 缓存系统中==操作失败=======");
      e.printStackTrace();
    }
  }
}